package com.seemsea.utils;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import com.seemsea.entity.admin.Page;

/**
 * @function ��ҳ
 * @author Emergency Team
 * @since 2009-6-3 
 */
public class PageString {
	
	/**
	 * @param request HttpServletRequest
	 * @param page ��ҳʵ��
	 * @return ��ҳ����
	 */
	
	public static String getPageString(HttpServletRequest request,Page page){
		StringBuffer sb = new StringBuffer();
		StringBuffer url=request.getRequestURL();
		StringBuffer parameter = new StringBuffer();
		url.append("?");
		Enumeration parameterNames = request.getParameterNames();
		while (parameterNames.hasMoreElements()) {
			String element = (String) parameterNames.nextElement();
			if(element.indexOf("page") > -1 || element.equals("rtFlag")) {
				continue;
			}
			parameter.append("&").append(element).append("=").append(request.getParameter(element));
		}
		int pageSize = page.getPageCount();
		int currentPage = page.getPage();
		int beforeShowPage = 2;
		int afterShowPage = 4;
		int start = -1;
		int end = -1;
		sb.append("<div class='page_div'>");
		if(pageSize <= beforeShowPage + afterShowPage)
		{
			start = 1;
			end = pageSize;
			
		}else{
			
			if(currentPage - beforeShowPage - 2 <= 0)
			{
				start = 1 ;
			}else{
				start = currentPage - beforeShowPage;
			}
			if(currentPage + afterShowPage >= pageSize)
			{
				end = pageSize;
			}else{
				end = currentPage + afterShowPage;
			}
		}
		if(currentPage > 1)
		{
			int prev = currentPage - 1;
			sb.append("<a href='").append(url).append("page=").append(prev).append(parameter).append("' class='page_prev'><div class='page_prev_able'></div><div class='page_prev_btn'>��һҳ</div></a>");
		}else{
			sb.append("<span class='span_dis'><div class='page_prev_dis'></div></span>");
		}
		if(start > 1 )
		{
			sb.append("<a href='").append(url).append("page=").append(1).append(parameter).append("' >" + 1 + "</a>");
			if(start > 2)
			{
				sb.append("<a href='").append(url).append("page=").append(2).append(parameter).append("' >" + 2 + "</a>");
				if(start > 3)
				{
					sb.append("<div class='page_etc'>...</div>");
				}
			}
		}
		
		for(int i=start;i<=end;i++)
		{
			if(i == currentPage)
			{
				sb.append("<span class='currentPage'>" + i + "</span>");
			}else{
				sb.append("<a href='").append(url).append("page=").append(i).append(parameter).append("'>" + i + "</a>");
			}
		}
		if(end < pageSize - 1 )
		{
			sb.append("<div class='page_etc'>...</div>");
		}
		if(end < pageSize)
		{
			sb.append("<a href='").append(url).append("page=").append(pageSize).append(parameter).append("' >" + pageSize + "</a>");
		}
		if(currentPage < pageSize)
		{
			int next = currentPage + 1;
			sb.append("<a href='").append(url).append("page=").append(next).append(parameter).append("' class='page_next'  ><div class='page_next_btn'>��һҳ</div><div class='page_next_able'></div></a>");
		}else{
			sb.append("<span class='span_dis'><div class='page_next_dis'></div></span>");
		}
		sb.append("<div class='page_ms'>��"+pageSize+"ҳ&nbsp;&nbsp;"+ page.getCount() +"��</div>");
		sb.append("</div>");
		return sb.toString();
	}
	
	public static String getPageString1(HttpServletRequest request,Page page){
		StringBuffer sb = new StringBuffer();
		StringBuffer url=request.getRequestURL();
		StringBuffer parameter = new StringBuffer();
		url.append("?");
		@SuppressWarnings("rawtypes")
		Enumeration parameterNames = request.getParameterNames();
		while (parameterNames.hasMoreElements()) {
			String element = (String) parameterNames.nextElement();
			if(element.indexOf("page") > -1 || element.equals("rtFlag")) {
				continue;
			}
			parameter.append("&").append(element).append("=").append(request.getParameter(element));
		}
		sb.append("����").append(page.getPageCount()).append("ҳ");
		sb.append(",").append(page.getCount()).append("����¼");	
		sb.append(",��ǰΪ��").append(page.getPage()).append("ҳ");
		sb.append(",<a href='").append(url).append("page=1").append(parameter).append("' class='urllink'>��ҳ</a>");
		if(page.getPage() > 1){
			sb.append(",<a href='").append(url).append("page=").append(page.getPage() - 1).append(parameter).append("' class='urllink'>��һҳ</a>");
		}
		if(page.getPage() < page.getPageCount()){
			sb.append(",<a href='").append(url).append("page=").append(page.getPage() + 1).append(parameter).append("' class='urllink'>��һҳ</a>");
		}
		if(page.getPageCount() == 0){
			sb.append(",<a href='").append(url).append("page=1").append(parameter).append("' class='urllink'>βҳ</a>");
		}else{
			sb.append(",<a href='").append(url).append("page=").append(page.getPageCount()).append(parameter).append("' class='urllink'>βҳ</a>");
		}
		sb.append(",��ת����");
		sb.append("<select id='page' name='page' onchange='jumpPage(this)' style='width:40px'>");
		for (int i = 1; i <= page.getPageCount(); i++) {
			if(i == page.getPage()){
				sb.append("<option value='").append(i).append("' selected>").append(i).append("</option>");
			}else{
				sb.append("<option value='").append(i).append("'>").append(i).append("</option>");
			}
		}
		sb.append("</select>ҳ");
		sb.append("<script>");
		sb.append("function jumpPage(obj){");
		sb.append("var flag;");
		//sb.append("var obj = document.getElementById('page');");
		sb.append("if(obj){flag = obj.options[obj.selectedIndex].value;}");
		sb.append("window.location.href='").append(url).append("page='+flag+'").append(parameter).append("';}");
		sb.append("</script>");
		return sb.toString();
	}
	
	public static String getAjaxPageString(HttpServletRequest request,Page page){
		StringBuffer sb = new StringBuffer();
		StringBuffer url=request.getRequestURL();
		StringBuffer parameter = new StringBuffer();
		url.append("?");
		Enumeration parameterNames = request.getParameterNames();
		while (parameterNames.hasMoreElements()) {
			String element = (String) parameterNames.nextElement();
			if(element.indexOf("page") > -1 || element.equals("rtFlag")) {
				continue;
			}
			parameter.append("&").append(element).append("=").append(request.getParameter(element));
		}
		int pageSize = page.getPageCount();
		int currentPage = page.getPage();
		int beforeShowPage = 2;
		int afterShowPage = 4;
		int start = -1;
		int end = -1;
		sb.append("<div class='page_div'>");
		if(pageSize <= beforeShowPage + afterShowPage)
		{
			start = 1;
			end = pageSize;
			
		}else{
			
			if(currentPage - beforeShowPage - 2 <= 0)
			{
				start = 1 ;
			}else{
				start = currentPage - beforeShowPage;
			}
			if(currentPage + afterShowPage >= pageSize)
			{
				end = pageSize;
			}else{
				end = currentPage + afterShowPage;
			}
		}
		if(currentPage > 1)
		{
			int prev = currentPage - 1;
			sb.append("<a href='javascript:;' class='page_prev' onclick='ajaxPage("+ prev +")' ><div class='page_prev_able'></div><div class='page_prev_btn'>��һҳ</div></a>");
		}else{
			sb.append("<span class='span_dis'><div class='page_prev_dis'></div></span>");
		}
		if(start > 1 )
		{
			sb.append("<a href='javascript:void(0);' onclick='ajaxPage(1)' >" + 1 + "</a>");
			if(start > 2)
			{
				sb.append("<a href='javascript:void(0);' onclick='ajaxPage(2)' >" + 2 + "</a>");
				if(start > 3)
				{
					//if(start == 4)
					//{
						//str += "<a href='javascript:void(0);' onclick='ajaxPage(3)' >" + 3 + "</a>";
					//}else{
						sb.append("<div class='page_etc'>...</div>");
					//}
				}
			}
		}
		
		for(int i=start;i<=end;i++)
		{
			if(i == currentPage)
			{
				sb.append("<span class='currentPage'>" + i + "</span>");
			}else{
				sb.append("<a href='javascript:void(0);' onclick='ajaxPage("+i+")' >" + i + "</a>");
			}
		}
		if(end < pageSize - 1)
		{
			sb.append("<div class='page_etc'>...</div>");
			
		}
		if(end < pageSize)
		{
			sb.append("<a href='javascript:void(0);' onclick='ajaxPage("+pageSize+")' >" + pageSize + "</a>");
		}
		
		if(currentPage < pageSize)
		{
			int next = currentPage + 1;
			sb.append("<a href='javascript:void(0);' class='page_next' onclick='ajaxPage(" + next + ")' ><div class='page_next_btn'>��һҳ</div><div class='page_next_able'></div></a>");
		}else{
			sb.append("<span class='span_dis'><div class='page_next_dis'></div></span>");
		}
		sb.append("<div class='page_ms'>��"+pageSize+"ҳ&nbsp;&nbsp;"+ page.getCount() +"��</div>");
		sb.append("</div>");
		return sb.toString();
		
	}
	
	public static String getAjaxPageStringForViewTr(HttpServletRequest request,Page page,String businessId){
		StringBuffer sb = new StringBuffer();
		StringBuffer url=request.getRequestURL();
		StringBuffer parameter = new StringBuffer();
		url.append("?");
		Enumeration parameterNames = request.getParameterNames();
		while (parameterNames.hasMoreElements()) {
			String element = (String) parameterNames.nextElement();
			if(element.indexOf("page") > -1 || element.equals("rtFlag")) {
				continue;
			}
			parameter.append("&").append(element).append("=").append(request.getParameter(element));
		}
		int pageSize = page.getPageCount();
		int currentPage = page.getPage();
		int beforeShowPage = 1;
		int afterShowPage = 1;
		int start = -1;
		int end = -1;
		sb.append("<div class='page_div'>");
		if(pageSize <= beforeShowPage + afterShowPage)
		{
			start = 1;
			end = pageSize;
			
		}else{
			
			if(currentPage - beforeShowPage - 2 <= 0)
			{
				start = 1 ;
			}else{
				start = currentPage - beforeShowPage;
			}
			if(currentPage + afterShowPage >= pageSize)
			{
				end = pageSize;
			}else{
				end = currentPage + afterShowPage;
			}
		}
		if(currentPage > 1)
		{
			int prev = currentPage - 1;
			//sb.append("<a href='javascript:;' class='page_prev' onclick='ajaxTrPage(\""+businessId+"\","+ prev +")' ><div class='page_prev_able'></div><div class='page_prev_btn'>��һҳ</div></a>");
		}else{
			sb.append("<span class='span_dis'><div class='page_prev_dis'></div></span>");
		}
		if(start > 1 )
		{
			sb.append("<a href='javascript:void(0);' onclick='ajaxTrPage(\""+businessId+"\",1)' >" + 1 + "</a>");
			if(start > 2)
			{
				sb.append("<div class='page_etc'>...</div>");
			}
		}
		
		for(int i=start;i<=end;i++)
		{
			if(i == currentPage)
			{
				sb.append("<span class='currentPage'>" + i + "</span>");
			}else{
				sb.append("<a href='javascript:void(0);' onclick='ajaxTrPage(\""+businessId+"\","+i+")' >" + i + "</a>");
			}
		}
		if(end < pageSize - 1)
		{
			sb.append("<div class='page_etc'>...</div>");
			
		}
		if(end < pageSize)
		{
			sb.append("<a href='javascript:void(0);' onclick='ajaxTrPage(\""+businessId+"\","+pageSize+")' >" + pageSize + "</a>");
		}
		
		if(currentPage < pageSize)
		{
			int next = currentPage + 1;
			//sb.append("<a href='javascript:void(0);' class='page_next' onclick='ajaxTrPage(\""+businessId+"\"," + next + ")' ><div class='page_next_btn'>��һҳ</div><div class='page_next_able'></div></a>");
		}else{
			sb.append("<span class='span_dis'><div class='page_next_dis'></div></span>");
		}
		sb.append("<div class='page_ms'>��"+pageSize+"ҳ&nbsp;&nbsp;"+ page.getCount() +"��</div>");
		sb.append("</div>");
		return sb.toString();
		
	}
	
	public static String getAjaxPageString1(HttpServletRequest request,Page page){
		StringBuffer sb = new StringBuffer();
		StringBuffer url=request.getRequestURL();
		StringBuffer parameter = new StringBuffer();
		url.append("?");
		Enumeration parameterNames = request.getParameterNames();
		while (parameterNames.hasMoreElements()) {
			String element = (String) parameterNames.nextElement();
			if(element.indexOf("page") > -1 || element.equals("rtFlag")) {
				continue;
			}
			parameter.append("&").append(element).append("=").append(request.getParameter(element));
		}
		sb.append("����").append(page.getPageCount()).append("ҳ");
		sb.append(",").append(page.getCount()).append("����¼");	
		sb.append(",��ǰΪ��").append(page.getPage()).append("ҳ");
		//sb.append(",<a href='").append(url).append("page=1").append(parameter).append("' class='urllink'>��ҳ</a>");
		sb.append(",<a href='javascript:void(0);' onclick='ajaxPage(1)' class='urllink'>��ҳ</a>");
		if(page.getPage() > 1){
			//sb.append(",<a href='").append(url).append("page=").append(page.getPage() - 1).append(parameter).append("' class='urllink'>��һҳ</a>");
			sb.append(",<a href='javascript:void(0);' onclick='ajaxPage("+(page.getPage()-1)+")' class='urllink'>��һҳ</a>");
		}
		if(page.getPage() < page.getPageCount()){
			//sb.append(",<a href='").append(url).append("page=").append(page.getPage() + 1).append(parameter).append("' class='urllink'>��һҳ</a>");
			sb.append(",<a href='javascript:void(0);' onclick='ajaxPage("+(page.getPage()+1)+")' class='urllink'>��һҳ</a>");
		}
		if(page.getPageCount() == 0){
			//sb.append(",<a href='").append(url).append("page=1").append(parameter).append("' class='urllink'>βҳ</a>");
			sb.append(",<a href='javascript:void(0);' onclick='ajaxPage(1)' class='urllink'>βҳ</a>");
		}else{
			//sb.append(",<a href='").append(url).append("page=").append(page.getPageCount()).append(parameter).append("' class='urllink'>βҳ</a>");
			sb.append(",<a href='javascript:void(0);' onclick='ajaxPage("+page.getPageCount()+")' class='urllink'>βҳ</a>");
		}
		sb.append(",��ת����");
		sb.append("<select name='page' onchange='ajaxPage(this.options[this.options.selectedIndex].value)' style='width:35px'>");
		for (int i = 1; i <= page.getPageCount(); i++) {
			if(i == page.getPage()){
				sb.append("<option value='").append(i).append("' selected>").append(i).append("</option>");
			}else{
				sb.append("<option value='").append(i).append("'>").append(i).append("</option>");
			}
		}
		sb.append("</select>ҳ");
		/*
		sb.append("<script>");
		sb.append("function jumpPage(){");
		sb.append("var flag;");
		sb.append("var obj = document.getElementById('page');");
		sb.append("if(obj){flag = obj.options[obj.selectedIndex].value;}");
		sb.append("search(flag);");
		//sb.append("window.location.href='").append(url).append("page='+flag+'").append(parameter).append("';}");
		//sb.append("window.location.href='").append(url).append("page='+flag+'").append(parameter).append("';}");
		sb.append("</script>");
		*/
		return sb.toString();
	}
}
